package com.qeegoo.b2bautozityp.moudle.detail.view;


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.CommonNavifatorAdapter;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DataSet;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.ExpandAdapter;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.LeftAdapter;
import com.qeegoo.b2bautozityp.moudle.detail.adapter.PageAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class InqueryFragment extends Fragment implements DataSet<List<DetailBean.GoodsInfoMoreListBean>>, View.OnClickListener, AdapterView.OnItemClickListener {
    private static final String TAG = "InqueryFragment";
    private ListView lv_left;
    private TextView total_amount;
    private Button btn_submit;
    private TextView tv_choose_type;


    private View header_expand;
    private MagicIndicator vp_indicator;
    private ViewPager view_pager;
    private PageAdapter pageAdapter;  //全局唯一的PagerAdapter
    private LeftAdapter leftAdapter;

    private List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreListBeans;
    private TextView tv_name;
    private TextView tv_count;
    private TextView tv_oem;
    private TextView btn_addComponent;
    private ExpandableListView expandLv;
    private ExpandAdapter expandAdapter;
    private View handleView;
    private View footer_expand;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_inquery, container, false);
        lv_left = (ListView) inflate.findViewById(R.id.lv_left);
        tv_choose_type = (TextView) inflate.findViewById(R.id.tv_choose_type);
        total_amount = (TextView) inflate.findViewById(R.id.total_amount);
        btn_submit = (Button) inflate.findViewById(R.id.btn_submit);


        //手动报价
        handleView = inflate.findViewById(R.id.handleView);
        vp_indicator = (MagicIndicator) inflate.findViewById(R.id.vp_indicator);
        view_pager = (ViewPager) inflate.findViewById(R.id.view_pager);
        //构造pageAdapter
        tv_choose_type.setText(getResources().getString(R.string.handle_price_detal));
        tv_choose_type.setOnClickListener(this);
        initIndicator();


        expandLv = (ExpandableListView) inflate.findViewById(R.id.expandLv);
//        历史报价
        header_expand = LayoutInflater.from(getActivity()).inflate( R.layout.header_expand,expandLv,false);
        footer_expand =LayoutInflater.from(getActivity()).inflate( R.layout.footer_expand,expandLv,false);

        tv_name = (TextView) header_expand.findViewById(R.id.tv_name);
        tv_count = (TextView) header_expand.findViewById(R.id.tv_count);
        tv_oem = (TextView) header_expand.findViewById(R.id.tv_oem);
        btn_addComponent = (TextView) footer_expand.findViewById(R.id.btn_addComponent);  //增加组件
        expandLv.setGroupIndicator(null);
        expandLv.addHeaderView(header_expand);
        expandLv.addFooterView(footer_expand);
        pageAdapter = new PageAdapter(getActivity());
        expandAdapter = new ExpandAdapter(btn_submit,pageAdapter,expandLv);
        pageAdapter.setExpandAdapter(expandAdapter);//                           pageAdapter与expandAdapter保留了相互的引用
        btn_addComponent.setOnClickListener(this);
        return inflate;
    }


    @Override
    public void setData(List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreListBeans) {
        this.goodsInfoMoreListBeans = goodsInfoMoreListBeans;

//        左边布局的设置
        leftAdapter = new LeftAdapter(total_amount, goodsInfoMoreListBeans);
        lv_left.setOnItemClickListener(this);
        lv_left.setAdapter(leftAdapter);


        view_pager.setAdapter(pageAdapter);            //viewPager设置了适配器
        pageAdapter.setDataAndRefreshed(0,goodsInfoMoreListBeans.get(0));  //pageAdapter设置了第一条数据


        expandLv.setAdapter(expandAdapter);                     //expandListView设置了适配器
//       int groupCount = expandAdapter.getGroupCount();
//        for (int i=0; i<groupCount; i++)
//        {
//            expandLv.expandGroup(i);
//        }


    }

    private void setChooseData(int position) {
        pageAdapter.setDataAndRefreshed(position,goodsInfoMoreListBeans.get(position));

    }

    private void initIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavifatorAdapter(view_pager));
        vp_indicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(getActivity(), 15);
            }
        });
        ViewPagerHelper.bind(vp_indicator, view_pager);
    }

    private boolean isHandleFlag = true;

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_choose_type) {
            if (isHandleFlag) {
                tv_choose_type.setText(getResources().getString(R.string.auto_price_detal));
                isHandleFlag = false;
                expandLv.setVisibility(View.VISIBLE);
                handleView.setVisibility(View.INVISIBLE);
            } else {
                tv_choose_type.setText(getResources().getString(R.string.handle_price_detal));
                isHandleFlag = true;
                expandLv.setVisibility(View.INVISIBLE);
                handleView.setVisibility(View.VISIBLE);
            }
        }else if(view.getId()==R.id.btn_addComponent) {
            expandAdapter.addItem();
        }

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        leftAdapter.setCurrentPositionAndRefreshed(position);
        setChooseData(position);
    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        picker.onActivityResult(getActivity(),requestCode, resultCode, data);
//    }


    public LeftAdapter getLeftAdapter(){
        return leftAdapter;
    }


}
